package org.lq.er.view.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Admin;
import org.lq.er.entity.Student;
import org.lq.er.service.AdminService;
import org.lq.er.service.StudentService;
import org.lq.er.service.impl.AdminServiceImpl;
import org.lq.er.service.impl.StudentServiceImpl;
import org.lq.er.view.admin.V_admin_manage;
import org.lq.er.view.student_info.ER_student_main;
import org.lq.er.view.register.V_register;
import org.lq.view.mian.V_Student;
import org.w3c.dom.css.RGBColor;

import com.mysql.cj.x.protobuf.MysqlxSession.Reset;
import com.sun.org.apache.xpath.internal.operations.And;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


public class V_login extends JFrame {

	private JPanel contentPane;
	private JTextField txt_cardId;
	private JPasswordField txt_pwd;
	private JRadioButton selectStudent;
	private JRadioButton selectTeacher;
	StudentService ss = new StudentServiceImpl();
	AdminService as = new AdminServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_login frame = new V_login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_login() {
		setTitle("登录");
		Image image = java.awt.Toolkit.getDefaultToolkit().getImage("image/register.jpg");
		this.setIconImage(image);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 788, 464);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		ImageIcon icon = new ImageIcon("image/loginbk.png");
		
		JLabel lblNewLabel = new JLabel(icon);
		lblNewLabel.setBounds(10, 10, 450, 405);
		contentPane.add(lblNewLabel);
		
		JPanel panel = new JPanel();
		panel.setBounds(470, 10, 292, 405);
		panel.setBackground(Color.white);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("欢迎");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(10, 25, 67, 51);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("请填入基本信息登录");
		lblNewLabel_2.setBounds(10, 91, 191, 25);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3_3 = new JLabel("身份证号");
		lblNewLabel_3_3.setBounds(10, 184, 53, 25);
		panel.add(lblNewLabel_3_3);
		
		JLabel lblNewLabel_3_4 = new JLabel("密码");
		lblNewLabel_3_4.setBounds(10, 231, 40, 25);
		panel.add(lblNewLabel_3_4);
		
		txt_cardId = new JTextField();
		txt_cardId.setColumns(10);
		txt_cardId.setBounds(89, 186, 193, 21);
		panel.add(txt_cardId);
		
		txt_pwd = new JPasswordField();
		txt_pwd.setBounds(89, 233, 193, 23);
		panel.add(txt_pwd);
		
		JButton btnNewButton_1 = new JButton("登录");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String cardId = txt_cardId.getText().toString().trim();
				String pwd = txt_pwd.getText().toString().trim();
				if(selectStudent.isSelected()) {
					Student student = ss.check(cardId, pwd);
					if(student!=null) {
						JOptionPane.showMessageDialog(V_login.this, ""
								+ "登录成功","提示",JOptionPane.INFORMATION_MESSAGE);
						
						V_Student v_student = new V_Student(student);
						v_student.setVisible(true);
						dispose();
						v_student.addWindowListener(new WindowAdapter() {
							@Override
							public void windowClosed(WindowEvent e) {
								V_login login = new V_login();
								login.setVisible(true);
								
							}
						}
							
						);
					}
					else {
						JOptionPane.showMessageDialog(V_login.this, "登录失败","提示",JOptionPane.ERROR_MESSAGE);
						reset();
					}
				}
				else {
					Admin admin = as.check(cardId, pwd);
					System.out.println(admin);
					if(admin!=null) {
						JOptionPane.showMessageDialog(V_login.this, "登录成功","提示",JOptionPane.INFORMATION_MESSAGE);
						V_admin_manage manage = new V_admin_manage(admin);
						manage.setVisible(true);
						dispose();
						manage.addWindowListener(new WindowAdapter() {
							@Override
							public void windowClosed(WindowEvent e) {
								V_login login = new V_login();
								login.setVisible(true);
								
							}
						}
							
						);
					}
					else {
						JOptionPane.showMessageDialog(V_login.this, "登录失败","提示",JOptionPane.ERROR_MESSAGE);
						reset();
					}
				}
			}
		});
		btnNewButton_1.setForeground(Color.WHITE);
		btnNewButton_1.setBackground(new Color(208, 32, 144));
		btnNewButton_1.setBounds(177, 314, 91, 34);
		panel.add(btnNewButton_1);
		
		selectStudent = new JRadioButton("学生");
		selectStudent.setSelected(true);
		selectStudent.setForeground(Color.BLACK);
		selectStudent.setBackground(Color.WHITE);
		selectStudent.setBounds(10, 134, 67, 25);
		panel.add(selectStudent);
		
		selectTeacher = new JRadioButton("管理员");
		selectTeacher.setForeground(Color.BLACK);
		selectTeacher.setBackground(Color.WHITE);
		selectTeacher.setBounds(89, 134, 67, 25);
		panel.add(selectTeacher);
		ButtonGroup bg = new ButtonGroup();
		bg.add(selectTeacher);
		bg.add(selectStudent);
		
		JLabel repasswd = new JLabel("忘记密码?");
		repasswd.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String cardId = txt_cardId.getText();
				if (!"".equals(cardId)){
					if(selectStudent.isSelected()) {
						Student student = ss.getByCard_id(cardId);
						V_repasswd repasswd = new V_repasswd(student);
						repasswd.setBounds(V_login.this.getX()+100, V_login.this.getY(), repasswd.getWidth(), repasswd.getHeight());
						repasswd.setVisible(true);	
					} else {
						Admin admin = as.getByCarId(cardId);
						V_repasswd2 repasswd2 = new V_repasswd2(admin);
						repasswd2.setBounds(V_login.this.getX()+100, V_login.this.getY(), repasswd2.getWidth(), repasswd2.getHeight());
						repasswd2.setVisible(true);	
					}
				} else {
					JOptionPane.showMessageDialog(V_login.this, "请输入身份证号和密码!","提示",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		repasswd.setBounds(223, 266, 59, 15);
		panel.add(repasswd);
		JButton btnNewButton_1_1 = new JButton("注册");
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_register register = new V_register();
				register.setVisible(true);
			}
		});
		btnNewButton_1_1.setForeground(Color.WHITE);
		btnNewButton_1_1.setBackground(new Color(208, 32, 144));
		btnNewButton_1_1.setBounds(31, 314, 91, 34);
		panel.add(btnNewButton_1_1);
		
	}
	// 重置内容
	public void reset() {
		selectStudent.setSelected(true);
		txt_cardId.setText("");
		txt_pwd.setText("");
	}
}
